package com.asa.mvvm_aop_frame.basic.aspect

import android.app.Activity
import android.util.Log
import com.asa.mvvm_aop_frame.basic.AppStackManager
import com.asa.mvvm_aop_frame.basic.pointcut.ActivityLifecyclePoint.ON_ACTIVITY_CREATE
import com.asa.mvvm_aop_frame.basic.pointcut.ActivityLifecyclePoint.ON_ACTIVITY_DESTROY
import com.asa.mvvm_aop_frame.basic.pointcut.ActivityLifecyclePoint.ON_ACTIVITY_PAUSE
import org.aspectj.lang.JoinPoint
import org.aspectj.lang.annotation.After
import org.aspectj.lang.annotation.Aspect
import org.aspectj.lang.annotation.Before

/**
 * create By：anderson
 * on 2022/4/27
 * desc:
 */
@Aspect
class AppStackAspect {

    @After(ON_ACTIVITY_CREATE)
    fun onActivityCreateAspect(joinPoint: JoinPoint){
        if (joinPoint.`this` is Activity){
            AppStackManager.instance.addActivity(joinPoint.`this` as Activity)
        }

    }


    @After(ON_ACTIVITY_PAUSE)
    fun onActivityPause(joinPoint: JoinPoint){
        if (joinPoint.`this` is Activity){
            val activity = joinPoint.`this` as Activity
            if (activity.isFinishing){
                AppStackManager.instance.removeActivity(activity)
            }
        }
    }


    @After(ON_ACTIVITY_DESTROY)
    fun onActivityDestroyedAspect(joinPoint: JoinPoint){
        if (joinPoint.`this` is Activity){
            val activity = joinPoint.`this` as Activity
            AppStackManager.instance.removeActivity(activity)
        }
    }

}